--全局的比较零散的函数，放在这里,被全局脚本包含 

--管理每日记录数据的key
DayAnswerCount = "dayAnswerCount"
ActorSmithCount = "actorSmithCount"
DayWishBottle = "daywishbottle"
FireworkGiveExp = "fireworkGiveExpCount"
MyBottleCount = "MySelfBottleCount"
FestivalFire = "FestivalFireCnt"

--玩家开始游戏要执行的参数
function startPlay(sysarg)
	--添加初始的任务

	local sceneId = Actor.getSceneId(sysarg)
	local qId = FirstEnterQuestMap[sceneId]

	if qId then
		Actor.addQuest(sysarg, qId)
	else
		print("sceneId====",sceneId)
	end	
end

--检查活动礼包各种卡
--[[
	4、内测新手卡；
	5、好评卡（100万张）；
	6、VIP特权礼包（5万张）；
	7、王者礼包（5万张）；
--]]
function checkNewPlayerNumber(sysarg,number,typeid)
	checkPlayerNumber(sysarg,number,typeid)
end

function checkPlayerNumber(sysarg,number,type)
	if (not number) then return false end
	--设置动态变量 用于判断输入窗口类型 
	local sDvar = Actor.getDyanmicVar(sysarg)
	--sDvar.getRewardCodeBag = tonumber(type)  --增值类型  用于限制每个按钮只能固定输入类型 

	sDvar.getRewardCodeBag = 0
	Actor.queryServicesAddCard(sysarg, number, true)
end

--传送到某个场景
function telportScene(sysarg,sceneid,x,y)
	System.telportScene(sysarg,sceneid,x,y)
end

--打开购买某个物品的窗口
function buyThisItem(sysarg,itemId)
	Actor.openDialogs(sysarg,diBuyAnItem,itemId)
end

--什么都不执行的函数，是空函数，不要删除
function emptyFunc(sysarg)
end

--什么都不执行的函数，是空函数，不要删除
function cancelFunction(sysarg)
end


function ResetDayData(sysarg)
	local sVar = Actor.getStaticVar(sysarg)
	sVar.TodayFalgs = {}
end

--获取当天的一些标记
function getTodayFlag( sysarg, key )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.TodayFalgs then
		sVar.TodayFalgs = {}
	end
	return sVar.TodayFalgs[key]
end

--设置当天的一些标记
function setTodayFlag(sysarg, key, value )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.TodayFalgs then
		sVar.TodayFalgs = {}
	end
	sVar.TodayFalgs[key] = value
end


-- 现时是否在时间段内
-- 时间是个table， {year=2013, month=2, day=17, hour=0,min=0,sec=0}
function CheckTime(starTime, endTime)	
	if  ((not starTime) or  (not endTime)) then return false; end
	return System.isInDateRange(starTime.year,starTime.month,starTime.day,starTime.hour,starTime.min,starTime.sec,
								endTime.year,endTime.month,endTime.day,endTime.hour,endTime.min,endTime.sec);
end

--检查背包格子数是否够
function gCheckBagGrids( sysarg, rewardCfg)
	local count = 0
	for k, v in ipairs( rewardCfg or {} ) do
		if v.type == 0 then
			count = count + Item.getAddItemNeedGridCount( sysarg, v.id, v.count, v.quality or 0, v.strong or 0)
		end
	end
	local bagIdxs = Item.getBagEmptyGridCount( sysarg )
	if bagIdxs < count then
		Actor.sendTipmsg( sysarg,string.format(Lang.ScriptTips.tf00008, count), ttFlyTip)
		return false
	end
	return true
end

---------------------------------------------快速传送到NPC begin-----------------------------------------------
-- 移动到目标
function moveToTarget(sysarg, sceneName, pos_x, pos_y)
	local oldSceneId = Actor.getSceneId(sysarg)

	--禁止速传物品
	local pScene = Fuben.getScenePtrByName(sceneName)
	if Fuben.hasMapAreaAttri(pScene, pos_x, pos_y, aaNotItemTran) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.mt00072, ttFlyTip)
		return false;
	end

	if Actor.hasState(sysarg,esStateDeath) then
		return false;
	end
	
	--正在押镖
	if Actor.getIsDartFlag(sysarg) == true then
		Actor.sendTipmsg(sysarg, Lang.Activity.g00030, ttFlyTip)
		return false;
	end
  
	local Fesult = System.telportSceneByName(sysarg, sceneName, pos_x, pos_y, 1)
	SpecialScene(sysarg, oldSceneId)
	if Fesult == false then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0008, ttFlyTip)
		return false;
	end
	Actor.triggerAchieveEvent(sysarg, 69, 0, 0, 0) -- 触发成就事件
	return true;
end


-- 是否在场景里 true 在, flase不在
function isInScene(sysarg, movePoints)
	if not movePoints then
		return false;
	end
	
	local sceneid = Actor.getSceneId(sysarg);
	for i=1, table.getn(movePoints) do
		if movePoints[i] == 2 then
			return false;
		end
		
		if movePoints[i] == sceneid then
			return true;
		end
	end
	return false;
end


-- 获得传送配置索引
function getChuanSongPointData(id)
	for i=1, #ChuansongPoint do
			if ChuansongPoint[i].id == id then
				return ChuansongPoint[i]
			end
	end
	return nil;
end


function questTargetSpaceMoveTo(sysarg, id)
	if not id or id <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0001, ttFlyTip);
		return;
	end
	
	--护送皇杆
	local var = Actor.getStaticVar(sysarg)
	if var.ybstate == 1 then
		Actor.sendTipmsg(sysarg,Lang.Activity.k00046, ttFlyTip) 
		return false
	end
	
	--摆摊
	if Actor.hasState(sysarg,esStateStall) then
		Actor.sendTipmsg(sysarg,Lang.stall001.zf137,ttFlyTip)
		return false
	end
	
	--在副本
	if Actor.getFubenId(sysarg) > 0 then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.zf138,ttFlyTip)
		return false 
	end

	--在武林争霸中
	if Actor.getSceneId(sysarg) == 50 then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.zf138,ttFlyTip)
		return false
	end
	
	local cfg = getChuanSongPointData(id);
	if not cfg then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0001, ttFlyTip);
		return;
	end

	if not cfg.movePointId then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0001, ttFlyTip);
		return;
	end

	if isInScene(sysarg, cfg.movePointId) == true then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0004, ttFlyTip);
		return;
	end

	local limit = cfg.condLimit;
	if limit.lv > 0 then
		local pLv = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL);
		if pLv < limit.lv then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0005, ttFlyTip);
			return;
		end
	end

	if limit.circle > 0 then
		local circle = Actor.getIntProperty(sysarg, PROP_ACTOR_CIRCLE);
		if circle < limit.circle then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0006, ttFlyTip);
			return;
		end
	end
	
	if limit.viplv > 0 then
		local viplv = Actor.getVipGrade(sysarg)
		if viplv < limit.viplv then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0006, ttFlyTip);
			return;
		end
	end
	
	local scenename = Fuben.getSceneNameById(cfg.sceneId);
	local pos_x, pos_y = Actor.getNpcPos(cfg.sceneId, cfg.ncpid, 0, 0);

	if moveToTarget(sysarg, scenename, pos_x, pos_y - 1) == true then
		-- 传送成功, 并打开npc
		local npcPtr = Fuben.getNpcPtrById(cfg.sceneId, cfg.ncpid);
		if not npcPtr then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0001, ttFlyTip);
			return;
		end
		Actor.OnNpcTalk(sysarg, npcPtr, "");
	end
end
---------------------------------------------快速传送到NPC end-----------------------------------------------


--boss令结束
function OnUseTokenBossEnd(bossarg, actorId)
	if Actor.hasState(bossarg,esStateDeath) then
		return
	end
	if Actor.getIntProperty(bossarg, PROP_CREATURE_HP) <= 0 then
		return
	end
	local hBoss = Actor.getHandle(bossarg)
	Fuben.clearMonster(hBoss)
	sysarg = Actor.getActorById(actorId) 
	if sysarg then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.UseBossToken003, ttFlyTip )
	end
end

